/**
 * 小地图点击
 */

import * as THREE from '../threejs/build/three.module.js';

class RaySmall {
    constructor(smallView, fly, points) {
        this.smallView = smallView;
        this.fly = fly;
        this.points = points;

        this.viewerContainerId = '#small';
        this.viewerContainer = $(this.viewerContainerId)[0];
    }

    start() {
        this.viewerContainer.addEventListener('click', e => this.ray(e));
    }

    ray(e) {
        let Sx = e.clientX - 9;
        let Sy = e.clientY - 9;

        let x = (Sx / $(this.viewerContainerId).width()) * 2 - 1;
        let y = -(Sy / $(this.viewerContainerId).height()) * 2 + 1;

        let standardVector = new THREE.Vector3(x, y, 0.5);

        let worldVector = standardVector.unproject(this.smallView.camera_s);

        let ray = worldVector.sub(this.smallView.camera_s.position).normalize();

        let raycaster = new THREE.Raycaster(this.smallView.camera_s.position, ray);

        let intersects = raycaster.intersectObjects(this.points);
        if (intersects.length > 0) {
            let rayObjct = intersects[0].object;

            this.fly.moveCameraOnly(rayObjct.flyPosition, rayObjct.flyTarget);
        }
    }
}

export { RaySmall }
